package com.chaosyeb.server.controller;

import com.chaosyeb.server.pojo.Position;
import com.chaosyeb.server.pojo.RespBean;
import com.chaosyeb.server.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @Description : 职位Controller
 * @Author : Chaos
 * @Date : 2021/12/8 21:33
 * @Version : 1.0
 **/
@RestController
@RequestMapping("/system/cfg/pos")
public class PositionController {

    @Autowired
    private IPositionService positionService;


    /**
     * 对职位的增删改查
     */
    @ApiOperation(value = "增加职位信息")
    @PostMapping("/add")
    public RespBean add(@RequestBody Position position){
        //添加时间--->LocalDateTime.now()获取当前时间，和之前的有区别
        position.setCreateDate(LocalDateTime.now());
        if (positionService.save(position)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "删除职位")
    @DeleteMapping("/delete/{id}")
    public RespBean delete(@PathVariable Integer id){
        if (positionService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "更新职位信息")
    @PutMapping("/update")
    public RespBean update(@RequestBody Position position){
        if (positionService.updateById(position)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "查询职位信息")
    @GetMapping("/find")
    public List<Position> find(){
        return positionService.list();
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/deleteIds")
    public RespBean deleteByIds(Integer[] ids){
        if (positionService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除成功");
        }
        return RespBean.error("批量删除失败");
    }
}
